import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'profitAndLossM',

  state: {
    timeData: [],
    tableData: [],
    PnlData: [],
    assetAmount: [],
    echartsInfo: [],
  },

  effects: {
    //
    *findProductTime({ payload, callback }, { call, put }) {
      const response = yield call(api.profitAndLoss.findProductTime, payload);
      if (response && response.data && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            timeData: response,
          },
        });
        callback(response);
      } else {
        message.error((response && response.msg) || '获取时间范围失败');
      }
    },

    //
    *getTableData({ payload, callback }, { call, put }) {
      const response = yield call(api.profitAndLoss.getTableData, payload);

      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            tableData: response,
          },
        });
        callback(response);
      } else {
        message.error(response.msg);
      }
    },
    //获取十点盈亏数据
    *getPnlData({ payload, callback }, { call, put }) {
      const response = yield call(api.profitAndLoss.getPnlData, payload);

      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            PnlData: response,
          },
        });
        callback(response);
      } else {
        callback('fail');
        message.error(response.msg);
      }
    },

    //获取区间维度信息
    *getEchartDate1({ payload, callback }, { call, put }) {
      const response = yield call(api.profitAndLoss.getEchartDate1, payload);

      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            PnlData: response,
          },
        });
        callback(response, 'sucess');
      } else {
        callback('faill');
        message.error(response.msg);
      }
    },

    //获取资产维度统计信息
    *getEchartDate2({ payload, callback }, { call, put }) {
      const response = yield call(api.profitAndLoss.getEchartDate2, payload);

      if (response && response.code == 0) {
        yield put({
          type: 'save',
          payload: {
            assetAmount: response,
          },
        });
        callback(response);
      } else {
        message.error(response.msg);
      }
    },

    *getEchartInfo({ payload, callback }, { call, put }) {
      const response = yield call(api.profitAndLoss.getEchartInfo, payload);

      if (response && response.code == 0) {
        // yield put({
        //   type: 'save',
        //   payload: {
        //     echartsInfo:response,
        //   },
        // });
        callback(response);
      } else {
        message.error(response.msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
